package lkj.action.admin;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import lkj.domain.admin.Admin;
import lkj.domain.user.User;
import lkj.service.admin.AdminService;
import lkj.service.user.UserService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lkj10 on 2017/3/12.
 */
@Controller
@Scope("prototype")//使用此注解，spring可以创建多个对象，默认情况只创建一个
public class AdminAction extends ActionSupport implements ModelDriven<Admin>{

    private  Admin model =new Admin();
    @Resource
    private AdminService adminService;
    @Resource
    private UserService userService;

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String loginUI(){
        return "loginUI";

    }

    public String login(){

        Admin admin=adminService.findByUser(model.getUsername(),model.getPassword());
        ActionContext.getContext().getSession().put("admin",admin);
        if (admin!=null){

            return "loginSuccess";
        }else {

            this.addActionError("用户名或密码错误");
            return "loginFail";
        }

    }
        public String allUser(){
            List<User> userList =userService.findAllUser();
            ActionContext.getContext().put("userList",userList);
        return "allUser";
        }

        public String delUser(){
            User user=userService.findId(model.getUid());
            userService.delete(user);
            return "toUserList";
        }
    @Override
    public Admin getModel() {
        return model;
    }
}
